﻿//BestSeller.aspx.cs
////////////////////////////////////////
////////////////////////////////////////
//当点击首页的热门商品时显示此页，即显示所有的热门商品
////////////////////////////////////////
////////////////////////////////////////
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class Bestseller : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //页面首次加载时,将当前页码设置为"1",并保存到Session中
            this.lbPage.Text = "1";
            Session.Add("currentPage", "1");
            this.BindInstruments();
        }
        if (Session["currengPage"] != "")
            this.BindInstruments();  //调用自定义方法绑定DataList控件,按条件显示商品列表
    }
    private void BindInstruments()
    {
        DataTable dtHotGood = new DataTable();
        ///////////////////////////////
        ///////////////////////////////
        //得到HotGood的所有商品列表,放到dtHotGood中。
        ///////////////////////////////
        ///////////////////////////////

        //使用PagedDataSource类在DataList控件中实现分页
        PagedDataSource ps1 = new PagedDataSource();
        ps1.DataSource = dtHotGood.DefaultView;
        ps1.AllowPaging = true;
        ps1.PageSize = 6;
        ps1.CurrentPageIndex = Convert.ToInt32(Session["currentPage"]) - 1;
        this.dlHotGood.DataSource = ps1;
        this.dlHotGood.DataBind();    //与图中DataList控件dlHotGood绑定
        //获取总页数
        this.lbAllPage.Text = Convert.ToString(ps1.PageCount);
        //若当前页是第1页，则禁用“首页”“上一页”按钮
        if (Convert.ToInt32(Session["currentPage"]) == 1)
        {
            this.lbtnFormerPage.Enabled = false;
            this.lbtnFirstPage.Enabled = false;
        }
        else
        {
            this.lbtnFirstPage.Enabled = true;
            this.lbtnFormerPage.Enabled = true;
        }
        //若当前页是最后一页，则禁用“下一页”“尾页”按钮
        if (Convert.ToInt32(Session["currentPage"]) == ps1.PageCount)
        {
            this.lbtnNextPage.Enabled = false;
            this.lbtnLastPage.Enabled = false;
        }
        else
        {
            this.lbtnLastPage.Enabled = true;
            this.lbtnNextPage.Enabled = true;
        }
    }
    //单击“首页”
    protected void lbtnFirstPage_Click(object sender, EventArgs e)
    {
        Session["currentPage"] = "1";
        this.lbPage.Text = "1";
        this.BindInstruments();
    }
    //单击“上一页”
    protected void lbtnFormerPage_Click(object sender, EventArgs e)
    {
        Session["currentPage"] = Convert.ToInt32(Session["currentPage"]) - 1;
        this.lbPage.Text = Session["currentPage"].ToString();
        this.BindInstruments();
    }
    //单击“下一页”
    protected void lbtnNextPage_Click(object sender, EventArgs e)
    {
        Session["currentPage"] = Convert.ToInt32(Session["currentPage"]) + 1;
        this.lbPage.Text = Session["currentPage"].ToString();
        this.BindInstruments();
    }
    //单击“尾页”
    protected void lbtnLastPage_Click(object sender, EventArgs e)
    {
        Session["currentPage"] = this.lbAllPage.Text;
        this.lbPage.Text = this.lbAllPage.Text;
        this.BindInstruments();
    }
}
